﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Entity.DomainModels.WorkforceManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.WorkforceManage;

namespace VOL.SmartMecial.Services.WorkforceManage
{
    public class ClassBedManageService:IClassBedManageService
    {
        /// <summary>
        /// 管床班次仓储
        /// </summary>
        private readonly IBaseRepository<ClassBedManageModel> classRepo;

        /// <summary>
        /// 人员分组仓储
        /// </summary>
        private readonly IBaseRepository<NurseGroupModel> nurseGroupRepo;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="classRepo">管床班次仓储</param>
        /// <param name="nurseGroupRepo">人员分组仓储</param>
        public ClassBedManageService(
            IBaseRepository<ClassBedManageModel> classRepo, 
            IBaseRepository<NurseGroupModel> nurseGroupRepo)
        {
            this.classRepo = classRepo;
            this.nurseGroupRepo = nurseGroupRepo;
        }

        public async Task<int> AddClassBedManages(ClassBedManageModel classBedManageModel)
        {
            return await classRepo.AddAsync(classBedManageModel);
        }

        /// <summary>
        /// 获取管床班次列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ClassBedManageDTO>> GetClassBedManages()
        {
            var query = from cls in classRepo.GetAll()
                        join nurse in nurseGroupRepo.GetAll() on cls.ClassGroupId equals nurse.Id
                        where cls.IsDelete == 0
                        select new ClassBedManageDTO
                        {
                            Id = cls.Id,
                            ClassGroupId = cls.ClassGroupId,
                            Bed = cls.Bed,
                            GroupName = nurse.GroupName
                        };

            return await Task.FromResult(query.ToList());
        }


        public async Task<int> DeleteClassBedManage(int id)
        {
            var entity = classRepo.GetAll().FirstOrDefault(x => x.Id == id);
            if (entity == null)
                return 0;
            await classRepo.Remove(entity);
            return 1;
        }

        public async Task<int> UpdateClassBedManage(ClassBedManageModel classBedManageModel)
        {
            var existingEntity = classRepo.GetAll().FirstOrDefault(x => x.Id == classBedManageModel.Id);
            if (existingEntity == null)
                return 0;

            existingEntity.ClassGroupId = classBedManageModel.ClassGroupId;
            existingEntity.Bed = classBedManageModel.Bed;
            
            await classRepo.UpdateAsync(existingEntity);
            return 1;
        }
    }
}
